% This code exports estimation results. 
clear;

% specify accordingly
root_dir = '...';
folder_tex_est = '...';

%% Table A.4: Consumer type distribution and their formal LTC risk
temp = load(fullfile(root_dir, 'matlab/demand_estimation_AK/demand_results_all.mat'));

pr_fc = temp.pr_fc;
pr_y = temp.pr_y;
clear temp

file_tex  = fullfile(folder_tex_est,'type_dist.tex');
FID = fopen(file_tex, 'w');
fprintf(FID, '\\begin{table} \n');
fprintf(FID, '\\begin{center} \n');
fprintf(FID, '\\scalebox{1.0}{ \n');
fprintf(FID, '\\begin{tabular}{llcc} \\hline \n');
fprintf(FID, ' & & (1) & (2)  \\\\ \n');
fprintf(FID, 'Consumer income & Availability of family care & Formal LTC risk & Share  \\\\ \n');
fprintf(FID, '\\hline \n');
fprintf(FID, 'Low & Not available & %10.2f & %10.2f \\\\ \n', pr_fc(1), pr_y(1));
fprintf(FID, 'Middle & Not available & %10.2f & %10.2f \\\\ \n', pr_fc(2), pr_y(2));
fprintf(FID, 'High & Not available & %10.2f & %10.2f \\\\ \n', pr_fc(3), pr_y(3));
fprintf(FID, 'Low & Available & %10.2f & %10.2f \\\\ \n', pr_fc(4), pr_y(4));
fprintf(FID, 'Middle & Available & %10.2f & %10.2f \\\\ \n', pr_fc(5), pr_y(5));
fprintf(FID, 'High & Available & %10.2f & %10.2f \\\\ \n', pr_fc(6), pr_y(6));
fprintf(FID, '\\hline \n');
fprintf(FID, '\\end{tabular} \n');
fprintf(FID, '} \n');
fprintf(FID, '\\end{center} \n');
fprintf(FID, '\\caption{Formal LTC risk by consumer type \\newline \\small {\\emph{Notes}: Column (1) reports the probability of using formal LTC services in the second stage for each consumer type. Consumer type is defined by income tercile and availability of family care. Column  (2) reports the share of each consumer type.}} \n');
fprintf(FID, '\\label{table.consumer.type} \n');
fprintf(FID, '\\end{table} \n');

%% Table 1: Demamd estimates
temp = load(fullfile(root_dir, 'matlab/demand_estimation_AK/demand_results_all.mat'));
alpha = temp.alpha;
SE_alpha = temp.SE_alpha;
elas_p_mkt = temp.elas_p_mkt;
elas_p = temp.elas_p;
pr_inside_y = temp.pr_inside_y;
emp_demand_y = temp.emp_demand_y;
pr_inside_ic = temp.pr_inside_ic;
emp_demand_ic = temp.emp_demand_ic;

file_tex  = fullfile(folder_tex_est,'demand_estimates.tex');

FID = fopen(file_tex, 'w');
fprintf(FID, '\\begin{table} \n');
fprintf(FID, '\\begin{center} \n');
fprintf(FID, '\\scalebox{1.0}{ \n');
fprintf(FID, '\\begin{tabular}{lcc} \\hline \n');
fprintf(FID, ' &  Estimate & Std error \\\\ \n');
fprintf(FID, '\\hline \n');
fprintf(FID, 'Consumption utility scale  ($\\alpha$)  & %10.4f & %10.4f \\\\ \n', alpha, SE_alpha);
fprintf(FID, 'Mean demand elasticity & %10.4f &  \\\\ \n', mean(elas_p));
fprintf(FID, '\\hline \n');
fprintf(FID, '\\end{tabular} \n');
fprintf(FID, '} \n');
fprintf(FID, '\\end{center} \n');
fprintf(FID, '\\caption{Demand parameter estimates \\newline } \n');
fprintf(FID, '\\label{table_demand_estimates} \n');
fprintf(FID, '\\end{table} \n');

%% Demand model fit -> used along with Table A.4 to calcualte Table A.5
file_tex  = fullfile(folder_tex_est,'demand_fit.tex');

FID = fopen(file_tex, 'w');
fprintf(FID, '\\begin{table} \n');
fprintf(FID, '\\begin{center} \n');
fprintf(FID, '\\scalebox{1.0}{ \n');
fprintf(FID, '\\begin{tabular}{lcc} \\hline \n');
fprintf(FID, ' & (1) & (2) \\\\ \n');
fprintf(FID, 'LTCI coverage rate & Model & Data (HRS) \\\\ \\hline \n');
fprintf(FID, '\\emph{By income: } & & \\\\ \n');
fprintf(FID, '\\hspace{0.2cm} Low income  & %10.4f & %10.4f \\\\ \n', ...
    mean(pr_inside_y(:,1)), emp_demand_y(1));
fprintf(FID, '\\hspace{0.2cm} Middle income & %10.4f & %10.4f \\\\ \n', ...
    mean(pr_inside_y(:,2)), emp_demand_y(2));
fprintf(FID, '\\hspace{0.2cm} High income  & %10.4f & %10.4f \\\\ \n', ...
    mean(pr_inside_y(:,3)), emp_demand_y(3));
fprintf(FID, '\\emph{By family care availability: } & & \\\\ \n');
fprintf(FID, '\\hspace{0.2cm} Family care not available   & %10.4f & %10.4f \\\\ \n', ...
    mean(pr_inside_ic(:,1)), emp_demand_ic(1));
fprintf(FID, '\\hspace{0.2cm} Family care available   & %10.4f & %10.4f \\\\ \n', ...
    mean(pr_inside_ic(:,2)), emp_demand_ic(2));
fprintf(FID, '\\hline \n');
fprintf(FID, '\\end{tabular} \n');
fprintf(FID, '} \n');
fprintf(FID, '\\end{center} \n');
fprintf(FID, '\\caption{Demand fit  \\newline \\small {\\emph{Notes}: Column (1) reports model-predicted demand for LTCI conditional on income (first three rows) and conditional on availability of family care (last two rows). Column (2) reports empirical LTCI coverage rate observed in the HRS.}} \n');
fprintf(FID, '\\label{table_demand_fit} \n');
fprintf(FID, '\\end{table} \n');

%% Table 2: Supply estimates
temp = load(fullfile(root_dir, 'matlab/supply_estimation/supply_estimates.mat'));
cost2_1     = temp.cost2_1;
cost2_0_mat = temp.cost2_0_mat;
cost_rs     = temp.cost_rs;
D_unmerge   = temp.D_unmerge;
pr_s        = D_unmerge.pr_s;
s1 = D_unmerge.s1;
B1_f = temp.B1_f;

mkt = temp.D_input.mkt;
major = temp.D_input.major;

% rate adjustment cost
Ecost_rs = sum(pr_s.*cost_rs,2);
Ecost_rs_pe = Ecost_rs./D_unmerge.s1; % per-enrollee cost

% - weighted mean & variance
s1_normalized = s1./sum(s1); % note that we use shares not actual enrollment to normalize. 
Ecost_rs_pe_bar = sum(Ecost_rs_pe.*s1_normalized);
Ecost_rs_pe_std = sqrt(sum(((Ecost_rs_pe-Ecost_rs_pe_bar).^2).*s1_normalized));

% variable coeff
Ecost2_1 = sum(pr_s.*cost2_1,2);

% - weighted mean & variance
Ecost2_1_bar = sum(Ecost2_1.*s1_normalized);
Ecost2_1_std = sqrt(sum(((Ecost2_1-Ecost2_1_bar).^2).*s1_normalized));

% fixed cost
cost2_0_vec = cost2_0_mat(:,1); % all columns same

% - bring down to market level
cost2_0_mkt = zeros(max(mkt),1);
for mm=1:max(mkt)
    cost2_0_mm = cost2_0_vec(D_unmerge.mkt==mm); 
    cost2_0_mkt(mm) = unique(cost2_0_mm);
end

% min loss regulation cost
cost1_e = temp.cost1_e;
cost_ml = (1/B1_f)*temp.cost_ml; % annual cost
cost_ml_pe = cost_ml./D_unmerge.s1; % per-enrollee annual cost

mkt_pos     = zeros(max(mkt),1); % 1 if market mean cost is positive

for mm=1:max(mkt)

    ind_m = find(mkt==mm & major==1);
    ind_f = find(mkt==mm & major==0);
    
    cost1_e_sub_m = cost1_e(ind_m);
    cost1_e_sub_f = unique(cost1_e(ind_f));
    if length(cost1_e_sub_f)~=1
        disp('ERROR in minloss_estimation')
    end

    mkt_pos(mm) = (mean([cost1_e_sub_m; cost1_e_sub_f])>=0);

end

mkt_pos_all = zeros(size(cost1_e));
for ii=1:length(cost1_e)
    mm=mkt(ii);
    mkt_pos_all(ii) = (mkt_pos(mm)==1);
end

mkt_pos_all = ones(size(cost1_e));

% estimates that survive restrictions
cost1_e_cf = cost1_e(mkt_pos_all==1);
cost_ml_cf_pe = cost_ml_pe(mkt_pos_all==1);
s1_cf = s1(mkt_pos_all==1);

% - weighted mean & variance
s1_cf_normalized = s1_cf./sum(s1_cf); % note that we use shares not actual enrollment to normalize. 
cost1_e_cf_bar = sum(cost1_e_cf.*s1_cf_normalized);
cost1_e_cf_std = sqrt(sum(((cost1_e_cf -cost1_e_cf_bar).^2).*s1_cf_normalized));

cost_ml_cf_pe_bar = sum(cost_ml_cf_pe.*s1_cf_normalized);
cost_ml_cf_pe_std = sqrt(sum(((cost_ml_cf_pe -cost_ml_cf_pe_bar).^2).*s1_cf_normalized));

% entry cost
mean_cost_entry    = temp.mean_cost_entry;
std_cost_entry     = temp.std_cost_entry;
entry_exitflag_vec = temp.entry_exitflag_vec;

file_tex  = fullfile(folder_tex_est,'supply_estimates.tex');

FID = fopen(file_tex, 'w');
fprintf(FID, '\\begin{table} \n');
fprintf(FID, '\\begin{center} \n');
fprintf(FID, '\\scalebox{1.0}{ \n');
fprintf(FID, '\\begin{tabular}{lccc} \\hline \n');
fprintf(FID, 'Parameter & Notation & Mean & S.d. \\\\ \n');
fprintf(FID, '\\hline \n');
fprintf(FID, 'Rate adjustment cost & & &  \\\\ \n');
fprintf(FID, '\\hspace{0.2cm} : Fixed cost  & $c^0$  & %10.4f &  %10.4f  \\\\ \n', ...
    mean(cost2_0_mkt), std(cost2_0_mkt));
fprintf(FID, '\\hspace{0.2cm} : Variable cost coefficient  & $c^{1}_{jk}$  & %10.4f &  %10.4f  \\\\ \n', ...
    mean(Ecost2_1), std(Ecost2_1));
fprintf(FID, '\\hspace{0.2cm} : Per-enrollee annual cost  &   & %10.0f &  %10.0f \\\\ \n', ...
    mean(Ecost_rs_pe), std(Ecost_rs_pe));
fprintf(FID, 'Initial rate regulation cost & & &  \\\\ \n');
fprintf(FID, '\\hspace{0.2cm} : Coefficient ($10^{-1}$)  &  $c^l_j$ & %10.4f &  %10.4f \\\\ \n', ...
    mean(cost1_e_cf)/(10^(-1)), std(cost1_e_cf)/(10^(-1)));
fprintf(FID, '\\hspace{0.2cm} : Per-enrollee annual cost  &     & %10.0f &  %10.0f \\\\ \n', ...
    mean(cost_ml_cf_pe), std(cost_ml_cf_pe));
fprintf(FID, 'Mean entry cost &  $E[c^e]$   & %10.0f &  %10.0f \\\\ \n', ...
    mean(mean_cost_entry(entry_exitflag_vec>0)), std(mean_cost_entry(entry_exitflag_vec>0)));
fprintf(FID, '\\hline \n');
fprintf(FID, '\\end{tabular} \n');
fprintf(FID, '} \n');
fprintf(FID, '\\end{center} \n');
fprintf(FID, '\\caption{Supply parameter estimates \\newline} \n');
fprintf(FID, '\\label{table_supply_estimates} \n');
fprintf(FID, '\\end{table} \n');

